<?php
namespace App\Logic\SalesReport;
use App\Models\SalesReport\PaymentDetail as TPaymentDetail;
use App\Models\SalesReport\PaymentTNSummary as TPaymentTNSummary;
use App\Models\SalesReport\PaymentCNSummary as TPaymentCNSummary;
use App\Models\SalesReport\PaymentClassify as TPaymentClassify;
use Illuminate\Support\Facades\DB;
class PaymentNoneClassify
{
    public static function exePayment($date){
        ini_set('memory_limit', '200M');
        set_time_limit(0);
        try{
            $start = time();
            $start_memory = memory_get_usage();
            DB::beginTransaction();
            $headers = TPaymentTNSummary::headersField();
            $pc = TPaymentClassify::query()
                ->where('report_date',$date)
                ->selectRaw(TPaymentDetail::_getFields($headers,['classify_id']))
                ->groupBy(['classify_id'])
                ->get();
            foreach ($pc as $pd){
                $data = TPaymentClassify::_getData($pd);
                TPaymentCNSummary::query()->updateOrCreate(
                    [
                        'report_date' => $date,
                        'classify_id' => $pd->classify_id
                    ], $data
                );
            }
            DB::commit();
            $end = time();
            $end_memory = memory_get_usage();
            print_r(($end - $start).'--------'.(($end_memory - $start_memory)/1024/1024));
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
